package com.tebie.cloud.task.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebie.cloud.task.server.service.OrderSchedulerService;
import com.tebie.cloud.task.server.service.feign.*;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.DateUtils;
import com.tebiecloud.order.client.model.entity.OrderGoods;
import com.tebiecloud.order.client.model.entity.Orderr;
import com.tebiecloud.retail.client.constants.RetailConstants;
import com.tebiecloud.yunst.client.model.entity.YunAgentCollect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class OrderSchedulerServiceImpl implements OrderSchedulerService {

    @Autowired
    OrderServiceClient orderServiceClient;

    @Autowired
    OrderGoodsServiceClient orderGoodsServiceClient;

    @Autowired
    RetailOrderServiceClient retailOrderServiceClient;

    @Autowired
    MpweixinServiceClient mpweixinServiceClient;

    @Autowired
    YunStPaymentServiceClient yunStPaymentServiceClient;

    @Autowired
    ActGoodsRefServiceClient actGoodsRefServiceClient;

    @Autowired
    GoodsSpecificationServiceClient goodsSpecificationServiceClient;

    @Override
    public void doOrderStatusByNotPayJob(int status) {
        log.info("执行待支付订单关闭定时器");
        //查询1个小时前未支付的订单
        String time = DateUtils.getBeforeMinutes(5);//查询30分钟未支付的订单
        ResultBody<List<Orderr>> listResultBody = orderServiceClient.selectNotPayOrderList(1,time);
        if(listResultBody!=null && listResultBody.getCode() == ResultEnum.OK.getCode()){
            List<Orderr> orderrList = listResultBody.getData();
            if(orderrList!=null && orderrList.size()>0){
                for (Orderr orderr:orderrList){
                    //更新订单状态
                    orderServiceClient.updateOrder(orderr.getOrderId(),status);
                    //回滚余额和乖乖币
                    if(orderr.getUserId()!=null){
                        int coin = orderr.getPayMgg()==null?0:orderr.getPayMgg();
                        int amount = orderr.getPayBalances()==null?0:orderr.getPayBalances();
                        //去掉没有指定人的和没有使用余额支付的
                        if(orderr.getUserId()!=null && (amount!=0 || coin!=0))
                         mpweixinServiceClient.callBackPlusAmount(orderr.getOrderId(),orderr.getUserId(),amount,coin,5);
                    }
                    //更新支付状态
                    yunStPaymentServiceClient.updateYunAgentCollect(orderr.getOrderId(), RetailConstants.RETAIL_ORDER_CANCELL);//取消状态
                    //更新分销状态
                    retailOrderServiceClient.updateRetailOrder(orderr.getOrderId(),status);
                    ResultBody<List<OrderGoods>> orderGoodsResultBody = orderGoodsServiceClient.listOrderGoods(orderr.getOrderId());
                    if(orderGoodsResultBody!=null && orderGoodsResultBody.getCode() == ResultEnum.OK.getCode()){
                        List<OrderGoods> orderGoodsList = orderGoodsResultBody.getData();
                        if(orderGoodsList!=null && orderGoodsList.size()>0){
                            for(OrderGoods orderGoods:orderGoodsList){
                                //回滚库存
                                goodsSpecificationServiceClient.updateStockQty(
                                        orderGoods.getGoodsSpecificationId(),
                                        orderGoods.getAmount());
                            }
                        }

                    }
                }
            }

        }
    }
}
